import React from 'react';
import { Button, Form, Input, Checkbox,Tree,Row,Col, TreeSelect,message  } from 'antd';
import { injectIntl } from 'react-intl';
import axios from 'axios'


class departmentPicker extends React.Component {
    constructor(props) {
        super(props);
        this.common = this.props.intl.messages.common;
        this.state = {
            departmentTree: [],
        }
    }

    componentWillMount(){
        this.fetchApi()
    }

    fetchApi=()=>{
        axios.get('/api/org/list/qry',).then(res => {
            if (res && res.data) {
                var arr4Level=res.data.map(k=>k.level);
                var min=Math.min(...arr4Level);
                res.data.forEach(ele=>{
                    let parentId = ele.parentOrgId;
                    ele.title= ele.name;
                    ele.key= ele.id;
                    ele.value= ele.id;
                    let level = ele.level;
                    if(level!=min){
                        res.data.forEach(d=>{
                            if (d.id === parentId) {
                                let childArray = d.children;
                                if(!childArray){
                                    childArray = []
                                }
                                childArray.push(ele);
                                d.children = childArray;
                            }
                        })
                    }
                });
                let data = res.data.filter(ele => ele.level == min);
                console.log('最终等到的tree结构数据: ', data);
                this.setState({
                    departmentTree:data
                })
            }
        })
    };


    render(){
        return(
            <TreeSelect
                allowClear={true}
                dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
                treeData={this.state.departmentTree}
                {...this.props}
            />
        )
    }
}

export default injectIntl(departmentPicker);